package com.educate.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.educate.commonutils.JwtUtils;
import com.educate.commonutils.MD5;
import com.educate.educenter.entity.UcenterMember;
import com.educate.educenter.entity.vo.RegisterVo;
import com.educate.educenter.mapper.UcenterMemberMapper;
import com.educate.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.educate.servicebase.exceptionhander.EducateException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.Action;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author educate.wht
 * @since 2022-08-25
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    //登录的方法
    @Override
    public String login(UcenterMember ucenterMember) {
        //获取登录手机号和密码
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        //手机号和密码分空判断
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            throw new EducateException(20001,"登录失败");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember ucenterMember1 = baseMapper.selectOne(wrapper);
        //判断查出来的对象是否为空
        if(ucenterMember1==null){
            throw new EducateException(20001,"登录失败");
        }
        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密,再和数据库密码进行比较
        //加密方式MD5
        if(!MD5.encrypt(password).equals(ucenterMember1.getPassword())){
            throw new EducateException(20001,"登录失败");
        }
        //判断用户是否禁用
        if(ucenterMember1.getIsDeleted()){
            throw new EducateException(20001,"登录失败");
        }
        //登录成功
        //生成token字符串,使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(ucenterMember1.getId(), ucenterMember1.getNickname());


        return jwtToken;
    }

    //注册
    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//手机号
        String nickname = registerVo.getNickname();//昵称
        String password = registerVo.getPassword();
        //判断非空判断
        if(StringUtils.isEmpty(mobile)||StringUtils.isEmpty(code)||StringUtils.isEmpty(nickname)){
            throw new EducateException(20001,"注册失败");
        }
        //判断验证码
        //TODO

        //判断手机号是否重复,表里面存在相同手机号不进行添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            throw new EducateException(20001,"注册失败");
        }
        //数据添加到数据库中
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        member.setIsDeleted(false);//用户是否禁用
        member.setAvatar("http://edu-longyang.oss-cn-beijing.aliyuncs.com/2020/08/05/25f411209c8b44b9b003482b6265c3c9file.png");
        baseMapper.insert(member);



    }
    //根据openid进行查询
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper=new QueryWrapper();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
